package org.example.ymcc.consumer;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.example.ymcc.constant.Constants;
import org.example.ymcc.constant.MQConstants;
import org.example.ymcc.domain.MediaFile;
import org.example.ymcc.service.IMediaFileService;
import org.example.ymcc.service.impl.MediaFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "MediaConsumer",
        topic = MQConstants.MEDIA_VIDEO_UPLOAD_TOPIC,
        selectorExpression = MQConstants.MEDIA_VIDEO_UPLOAD_TAG
)
public class MediaConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String s = new String(body, Constants.UTF8);
            MediaFile mediaFile = JSONObject.parseObject(s, MediaFile.class);
            mediaFileService.handleFile2m3u8(mediaFile);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
